﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AtlantisRevenge.GamePlay
{
    public class Bullet : GameplayObject
    {
        Ship source;
        public Ship Source
        {
            get { return source; }
        }

        public Bullet(Ship source)
        {
            this.source = source;
            Initialize();
        }

        private Vector2 FindPosition()
        {
            if (source is Fleet)
                return new Vector2(source.Position.X + (source.Texture.Width / 2), source.Position.Y + (source.Texture.Height / 2));
            else
                return new Vector2(source.Position.X, source.Position.Y);
        }

        private Vector2 FindVelocity()
        {
            if (source is Fleet)
                return new Vector2(FromPixelPerLoop(9), 0);
            else
            {
                Vector2 velocity;
                velocity = new Vector2(source.Speed, source.Speed);
                velocity.Normalize();
                velocity = Vector2.Multiply(velocity, FromPixelPerLoop(9));
                return velocity;
            }
        }

        GameWindow window;

        public override void Initialize()
        {
            Texture = bulletTexture;
            Color = source.BulletColor;
            this.window = Window;

            Layer = source.Layer;
            Position = FindPosition();
            
            if (source.LookRight)
            {
                Velocity = FindVelocity();
            }
            else
            {
                Velocity = (FindVelocity()) * -1;
            }

            Rotation = (float)Math.Atan(Velocity.Y / Velocity.X);
            Animated = false;

            DieTime = TimeSpan.FromSeconds(0.35);
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            /*
            if (Position.X > window.ClientBounds.Width)
                Die();*/
            base.Update(gameTime);
        }

        public override void CollisionTRGT(GameplayObject target)
        {
            Die();
            if (target is Bullet)
                target.Die();
        }

        public static Texture2D bulletTexture;
    }
}